<?php
/**
 * RoomModel
 * @author tuanna
 * @version 1.0
 */
require_once 'Zend/Db/Table/Abstract.php';

class RoomModel extends Zend_Db_Table_Abstract
{
    protected $_name = 'room';
   
    public function addRoom($code, $type, $number_seat, $rent_price, $description){
    	$data = array(
    		'code' => $code,
    		'type' => $type,
    		'number_seat' => $number_seat,
    		'rent_price' => $rent_price,
    		'description' => $description
    	);
    	
    	return $this->insert($data);
    }
    
    public function updateRoom($id, $code, $type, $number_seat, $rent_price, $description){
    	$data = array(
    		'code' => $code,
    		'type' => $type,
    		'number_seat' => $number_seat,
    		'rent_price' => $rent_price,
    		'description' => $description
    	);
    	$check = $this->update($data, "id = $id");
    	
    	return $check;
    }
    
    public function updateRoomData($id, $data){
    	$check = $this->update($data, "id = $id");
    	
    	return $check;
    }
    
    public function deleteRoom($del_ids){
    	if($this->update(array('status' => 0), "id in ($del_ids)") > 0){
    		
    		return true;
    	}else{
    		
    		return false;
    	}
    }
    
    public function getListRoom($search_type = "all", $search_key = "", $number_seat = 0, $type = 0, $page = NULL, $line_per_page = NULL, $view_deleted = false)
    {
    	$db = $this->getAdapter();
    	$select = $this->select();
    	
    	//search by search_type & search_key
    	if($search_key){
	    	if($search_type == "all")
	    		$select->where("code like '%$search_key%' or description like '%$search_key%'");
	    	
	    	else if ($search_type == "code")
	    		$select->where("code like '%$search_key%'");
	    	
	    	else if ($search_type == "description")
	    		$select->where("description like '%$search_key%'");
    	}
    	if(!$view_deleted)
			$select->where('status = 1');
		
		//filter by number seat
		if($number_seat > 0)
			$select->where("number_seat = ?", $number_seat);
		
		//filter by room type
		if($type > 0)
			$select->where("type = ?", $type);
    	
    	//do paging
    	if($page && $line_per_page){
    		$select->limitPage($page, $line_per_page);
    	}
    	
    	//order
    	$select->order("code");
    	
    	$list = $db->fetchAll($select);
    	
    	return $list;
    }
    
    public function getListRoomCount($search_type = "all", $search_key="", $number_seat = 0, $type = 0)
    {
    	$db = $this->getAdapter();
    	$select = $this->select();
    	
    	//search by search_type & search_key
    	if($search_key){
	    	if($search_type == "all")
	    		$select->where("code like '%$search_key%' or description like '%$search_key%'");
	    	
	    	else if ($search_type == "code")
	    		$select->where("code like '%$search_key%'");
	    	
	    	else if ($search_type == "description")
	    		$select->where("description like '%$search_key%'");
    	}
		$select->where('status = 1');
		
		//filter by number seat
		if($number_seat > 0)
			$select->where("number_seat = ?", $number_seat);
		
		//filter by room type
		if($type > 0)
			$select->where("type = ?", $type);
    	
    	$list = $db->fetchAll($select);
    	
    	return count($list);
    }    	
    
  	public function fetchRoom($id){
    	$row = $this->fetchRow("id = $id and status = 1");
    	return $row;
    }
}